<?php
/*
* LoginView
* Handles views that are shown under login.
* Also handles clickevents and input.
*/
class LoginView
{
	// -------------------------------------- //
	/*
	* Functions
	* Various functions to show diffrent views depending on the event.
	*/
	
	/*
	* ShowLoginForm
	* @return string A html-loginform.
	*/
	public function doLoginForm()
	{
		$xhtml = '<form action="" method="post">
			<label for="username">Användarnamn:</label><br />
			<input type="text" name="username" maxlength="20" required tabindex="1"/><br />
			<label for="password">Lösenord:</label><br />
			<input type="password" name="password" required tabindex="2" /><br />
			<input type="submit" name="login" value="Logga in" tabindex="4" />
		</form>';
		
		return $xhtml;
	}
	
	/*
	* ShowLogoutButton
	* @return string Shows a logout button.
	*/
	public function doLogOutButton()
	{
		$xhtml = '
		<form action="" method="post">
			<input type="submit" name="logout" value="Logga ut" />
		</form>';
		
		return $xhtml;
	}
	
	/*
	* GetPostUsername
	* @return string The inputed Username
	*/
	public function GetPostUsername()
	{
		if ( isset( $_POST['username'] ) )
		{
			return $_POST['username'];
		}
	}
	
	/* GetPostPassword
	* @return string The inputed password sha1-encrypted.
	*/
	public function GetPostPassword()
	{
		// Is the password set
		if ( isset( $_POST['password'] ) )
		{
			return sha1( $_POST['password'] );
		}
	}
	
	// -------------------------------------- //
	/*
	* Eventhandlers
	* Handles various events like buttonclicks and validation.
	*/
	
	/*
	* triedToLogin 
	* Has the user tried to login.
	* @return bool True/False depending on if the needed values have been set.	
	*/
	public function triedToLogin()
	{
		if ( isset( $_POST['login'] ) && isset( $_POST['username'] ) && isset( $_POST['password']) )
		{
			return true;
		}
		
		return false;
	}
	
	/*
	* triedToLogout
	* Has the user clicked the logout-button
	* @return bool True if the user has clicked the button, else false.
	*/
	public function triedToLogout()
	{
		if ( isset( $_POST['logout'] ) )
		{
			return true;
		}
		return false;
	}
	
	/*
	* IsUserLoggedIn
	* Checks if the users is logged in.
	* @return bool True if the user is logged in. False if not.
	*/
	public function IsLoggedIn()
	{
		if ( isset( $_SESSION['username'] ) && isset( $_SESSION['password'] ) )
		{
			return true;
		}
		
		return false;
	}
	
	/*
	* Logout
	* Logsout the user and destorys the sessions and, if any, cookie.
	*/
	public function Logout()
	{
		unset( $_SESSION['username'] );
		unset( $_SESSION['password'] );
		
		header( "Location: index.php" );
	}
}